﻿Public Class repuestos


    Dim cadena As String = "Data Source=CESAR-PC\SQLSERVER;Initial Catalog=CAPCrepuestos;Integrated Security=True"
    Enum termino
        aprobado
        rechazado
    End Enum
    Dim accion As estado = estado.insertar

    Enum estado
        insertar
        modificar
    End Enum



    Private Sub cargarCombo(ByVal combo As ComboBox, ByRef datos As DataTable, ByVal pk As String, ByVal descripcion As String)

        combo.DataSource = datos
        combo.DisplayMember = descripcion
        combo.ValueMember = pk

    End Sub

    Private Function leo_tabla(ByVal nombreTabla As String) As Data.DataTable

        Dim conexion As New Data.SqlClient.SqlConnection
        Dim cmd As New Data.SqlClient.SqlCommand
        Dim tabla As New Data.DataTable

        conexion.ConnectionString = cadena
        conexion.Open()
        cmd.Connection = conexion

        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT * FROM " & nombreTabla
        tabla.Load(cmd.ExecuteReader())

        conexion.Close()

        Return tabla

    End Function

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click, btnSalir.Click
        txtCodigoInterno.Enabled = True
        txtCostoFinal.Enabled = True
        txtCostoSinIVA.Enabled = True
        txtDescripcion.Enabled = True
        txtFechaCosto.Enabled = True
        txtDescripcion.Enabled = True
        txtIVA.Enabled = True
        txtNroPieza.Enabled = True
        txtObservaciones.Enabled = True
        cmbAplicacion.Enabled = True
        cmbMarca.Enabled = True
        cmbProveedor.Enabled = True
        cmbRubro.Enabled = True

        btnModificar.Enabled = False

    End Sub

    Private Sub repuestos_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cargarCombo(cmbAplicacion, leo_tabla("aplicacion"), "id", "descripcion")
        cargarCombo(cmbMarca, leo_tabla("marca"), "id", "descripcion")
        cargarCombo(cmbRubro, leo_tabla("rubro"), "id", "descripcion")
        cargarCombo(cmbProveedor, leo_tabla("Proveedores"), "numero", "nombre_proveedor")



        txtCostoFinal.Enabled = False
        txtCostoSinIVA.Enabled = False
        txtDescripcion.Enabled = False
        txtFechaCosto.Enabled = False
        txtIVA.Enabled = False
        txtObservaciones.Enabled = False
        cmbMarca.Enabled = False
        'cmbModelo.Enabled = False
        cmbAplicacion.Enabled = False
        cmbProveedor.Enabled = False
        cmbRubro.Enabled = False

    End Sub

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        Dim conexion As New Data.SqlClient.SqlConnection
        Dim cmd As New Data.SqlClient.SqlCommand
        Dim tabla As New Data.DataTable

        conexion.ConnectionString = cadena
        conexion.Open()
        cmd.Connection = conexion
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT * FROM repuestos WHERE nro_pieza = " & txtNroPieza.Text

        tabla.Load(cmd.ExecuteReader())

        Me.txtFechaCosto.Text = tabla.Rows(0)("fecha_costo")
        Me.txtCodigoInterno.Text = tabla.Rows(0)("codigo_interno")
        Me.txtDescripcion.Text = tabla.Rows(0)("descripcion")
        Me.cmbAplicacion.SelectedValue = tabla.Rows(0)("aplicacion")
        Me.cmbMarca.SelectedValue = tabla.Rows(0)("marca")
        'Me.cmbModelo.SelectedValue = tabla.Rows(0)("modelo")
        Me.cmbProveedor.SelectedValue = tabla.Rows(0)("proveedor")
        Me.cmbRubro.SelectedValue = tabla.Rows(0)("rubro")
        Me.txtIVA.Text = tabla.Rows(0)("IVA")
        Me.txtObservaciones.Text = tabla.Rows(0)("observaciones")
        Me.txtCostoFinal.Text = tabla.Rows(0)("costo_final")
        Me.txtCostoSinIVA.Text = tabla.Rows(0)("costo_sin_iva")


        conexion.Close()
    End Sub

    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If Me.accion = estado.insertar Then
            insertar()
        Else
            modificar()
        End If
    End Sub
    Private Function insertar() As termino
        Dim conexion As New Data.SqlClient.SqlConnection
        Dim cmd As New Data.SqlClient.SqlCommand
        Dim consulta As String = ""

        conexion.ConnectionString = cadena
        conexion.Open()

        consulta = "insert into repuestos (nro_pieza,fecha_costo,codigo_interno,descripcion,aplicacion,rubro,proveedor,marca,precio_unitario,IVA,precio_final,observaciones)"
        consulta &= "values (" & txtNroPieza.Text
        consulta &= ", '" & Me.txtFechaCosto.Text & "'"
        consulta &= ", '" & Me.txtCodigoInterno.Text & "'"
        consulta &= ", '" & Me.txtDescripcion.Text & "'"
        consulta &= ", " & Me.cmbAplicacion.SelectedValue
        consulta &= ", " & Me.cmbRubro.SelectedValue
        consulta &= ", " & Me.cmbProveedor.SelectedValue
        consulta &= ", " & Me.cmbMarca.SelectedValue
        consulta &= ", " & Me.txtCostoSinIVA.Text
        consulta &= ", " & Me.txtIVA.Text
        consulta &= ", " & Me.txtCostoFinal.Text
        consulta &= ", '" & Me.txtObservaciones.Text & "')"


        cmd.CommandType = CommandType.Text
        cmd.CommandText = consulta
        cmd.Connection = conexion
        cmd.ExecuteNonQuery()
        conexion.Close()
        MsgBox("El repuesto fue guardado exitosamente", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ATENCION")
        Return termino.aprobado
    End Function

    Private Function modificar() As termino
        Dim conexion As New Data.SqlClient.SqlConnection
        Dim cmd As New Data.SqlClient.SqlCommand
        Dim modificacion As String = ""

        conexion.ConnectionString = cadena
        conexion.Open()

        modificacion = "UPDATE repuestos"
        modificacion &= " Set descripcion='" & Me.txtDescripcion.Text & "'"
        modificacion &= ", aplicacion=" & Me.cmbAplicacion.SelectedValue
        modificacion &= ", marca=" & Me.cmbMarca.SelectedValue
        modificacion &= ", proveedor=" & Me.cmbProveedor.SelectedValue
        modificacion &= ", rubro=" & Me.cmbRubro.SelectedValue
        modificacion &= ", precio_final=" & Me.txtCostoFinal.Text
        modificacion &= ", precio_unitario=" & Me.txtCostoSinIVA.Text
        modificacion &= ", observaciones='" & Me.txtObservaciones.Text & "'"
        modificacion &= " WHERE (numero_pieza=" & Me.txtNroPieza.Text & ")"

        cmd.Connection = conexion
        cmd.CommandType = CommandType.Text
        cmd.CommandText = modificacion

        cmd.ExecuteNonQuery()
        conexion.Close()

        Return termino.aprobado

    End Function

    Private Sub btnSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalir.Click
        Me.Close()

    End Sub

    Private Sub repuestos_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If (MsgBox("¿Desea salir?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Atencion") = MsgBoxResult.Yes) Then
            e.Cancel = False
        Else
            e.Cancel = True
        End If
    End Sub

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        Dim conexion As New Data.SqlClient.SqlConnection
        Dim cmd As New Data.SqlClient.SqlCommand
        Dim tabla As New Data.DataTable

        conexion.ConnectionString = cadena
        conexion.Open()
        cmd.Connection = conexion
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "DELETE FROM repuestos WHERE codigo_interno LIKE " & txtCodigoInterno.Text
        cmd.ExecuteNonQuery()
        conexion.Close()
    End Sub

    Private Sub btn_cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cancelar.Click
        Me.resetData()
        btnNuevo.Enabled = True
        btnModificar.Enabled = True
    End Sub
    Private Sub resetData()
        cmbAplicacion.SelectedIndex = -1
        cmbMarca.SelectedIndex = -1
        cmbProveedor.SelectedIndex = -1
        cmbRubro.SelectedIndex = -1
        txtCodigoInterno.Text = ""
        txtCostoFinal.Text = ""
        txtCostoSinIVA.Text = ""
        txtDescripcion.Text = ""
        txtFechaCosto.Text = ""
        txtIVA.Text = ""
        txtNroPieza.Text = ""
        txtObservaciones.Text = ""
        

        cmbAplicacion.Enabled = False
        cmbMarca.Enabled = False
        cmbProveedor.Enabled = False
        cmbRubro.Enabled = False
        txtCodigoInterno.Enabled = True
        txtCostoFinal.Enabled = False
        txtCostoSinIVA.Enabled = False
        txtDescripcion.Enabled = False
        txtFechaCosto.Enabled = False
        txtIVA.Enabled = False
        txtNroPieza.Enabled = True
        txtObservaciones.Enabled = False


    End Sub

    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click
        cmbAplicacion.Enabled = True
        cmbMarca.Enabled = True
        cmbProveedor.Enabled = True
        cmbRubro.Enabled = True
        txtCodigoInterno.Enabled = False
        txtCostoFinal.Enabled = True
        txtCostoSinIVA.Enabled = True
        txtDescripcion.Enabled = True
        txtFechaCosto.Enabled = True
        txtIVA.Enabled = True
        txtNroPieza.Enabled = False
        txtObservaciones.Enabled = True

        btnNuevo.Enabled = False
        btnEliminar.Enabled = False
    End Sub

    Private Sub txtNroPieza_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNroPieza.KeyPress
        If Not Char.IsDigit(e.KeyChar) And e.KeyChar <> vbBack Then e.Handled = True
        txtNroPieza.MaxLength = 6
    End Sub

    Private Sub txtCodigoInterno_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCodigoInterno.KeyPress
        If Not Char.IsLetterOrDigit(e.KeyChar) And e.KeyChar <> vbBack Then e.Handled = True
        txtCodigoInterno.MaxLength = 6
    End Sub

    
    Private Sub txtDescripcion_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDescripcion.KeyPress
        If Not Char.IsLetterOrDigit(e.KeyChar) And e.KeyChar <> vbBack And Asc(e.KeyChar) <> 32 Then e.Handled = True
        txtDescripcion.MaxLength = 45
    End Sub

    
    Private Sub txtCostoSinIVA_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCostoSinIVA.KeyPress
        If Not Char.IsDigit(e.KeyChar) And e.KeyChar <> vbBack And Asc(e.KeyChar) <> 32 And Asc(e.KeyChar) <> 46 Then e.Handled = True
        txtCostoSinIVA.MaxLength = 18
    End Sub


    
    Private Sub txtCostoFinal_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCostoFinal.KeyPress
        If Not Char.IsDigit(e.KeyChar) And e.KeyChar <> vbBack And Asc(e.KeyChar) <> 32 And Asc(e.KeyChar) <> 46 Then e.Handled = True
        txtCostoFinal.MaxLength = 18
    End Sub


    Private Sub txtIVA_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtIVA.KeyPress
        If Not Char.IsDigit(e.KeyChar) And e.KeyChar <> vbBack And Asc(e.KeyChar) <> 32 And Asc(e.KeyChar) <> 46 Then e.Handled = True
        txtIVA.MaxLength = 2
    End Sub

    Private Sub txtObservaciones_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtObservaciones.KeyPress
        If Not Char.IsLetterOrDigit(e.KeyChar) And e.KeyChar <> vbBack And Asc(e.KeyChar) <> 32 Then e.Handled = True
        txtDescripcion.MaxLength = 100
    End Sub
End Class